package com.baosight.risk.web.system;

import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.system.entity.SysMenu;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.api.system.ISysMenuService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author duanyongqiang
 * @Data 2019.3.29
 * <p>
 * 菜单管理
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService menuService;


    @GetMapping("list")
    public AjaxResult list(SysMenu menu) {
        Object data = menuService.selectMenuList(menu);
        return AjaxResult.success("查询成功", data);
    }

    /**
     * 删除菜单
     */
    @Log
    @GetMapping("/remove")
    public AjaxResult remove(String menuId) {
        if (menuService.selectCountMenuByParentId(menuId) > 0) {
            return error(1, "存在子菜单,不允许删除");
        }
        if (menuService.selectCountRoleMenuByMenuId(menuId) > 0) {
            return error(1, "菜单已分配,不允许删除");
        }
//        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(menuService.deleteMenuById(menuId));
    }

    /**
     * 新增
     */
    @GetMapping("/add/")
    public AjaxResult add(@RequestParam("parentId") String parentId) {
        SysMenu menu = null;
        if (!"0".equals(parentId)) {
            menu = menuService.selectMenuById(parentId);
        } else {
            menu = new SysMenu();
            menu.setMenuName("主目录");
        }
        return success(menu);
    }

    /**
     * 新增保存菜单
     */
    @Log(title = "菜单管理")
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody SysMenu menu) {
        menu = (SysMenu) BeanUtils.initBean(menu);
        if (StringUtils.isBlank(menu.getParentId())) {
            menu.setParentId("0");
        }
        menu.setVisible("0");
        menu.setStatus("10");
        return toAjax(menuService.insert(menu));
    }

    /**
     * 修改菜单
     */
    @GetMapping("/getItem")
    public AjaxResult edit(String id) {
        return success(menuService.selectMenuById(id));
    }

    /**
     * 修改保存菜单
     */
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody SysMenu menu) {
        if (StringUtils.isBlank(menu.getParentId())) {
            menu.setParentId("0");
        }
        return toAjax(menuService.update(menu));
    }

    /**
     * @return
     */
    @GetMapping("/menuData")
    public AjaxResult selectMenusByUser() {
        //获取当前登陆的用户
        XtzyUser user = getUser();
        if (user == null) {
            return AjaxResult.error("用户未登录");
        }
        List<Map<String, Object>> data = menuService.selectMenusByUser(user);
        return AjaxResult.success("查询成功", data);
    }
}